/**
 * CreateTime   : 2020年8月29日
 * Des          : UI层级管理
 */

import { find, Node } from "cc";
import { EnUILayer } from "../const/EnCommon";
import { EnUI } from "../const/EnUI";

 export class UILayerMgr
 {
    private static mgr : UILayerMgr;
    // private TopLayar:Node = new Node();
    // private NormalLayer:Node = new Node();
    // private BottomLayer:Node = new Node();
    private FrontLayer:Node = new Node();
    private TopLayar:Node;
    private NormalLayer:Node;
    private BottomLayer:Node;
    private stage : Node;
    private freeLayer = new Map<string, Node>();

    static get instance()
    {
        if(!this.mgr)
        {
            this.mgr = new UILayerMgr();
        }
        return this.mgr;
    }

    static destory() {
        if(this.mgr)
        {
            this.mgr = null
        }
    }

    OnInitLayerNode(bottomNode, normalNode, topNode)
    {
        this.BottomLayer = bottomNode;
        this.NormalLayer = normalNode;
        this.TopLayar = topNode;
    }

    OnInit(node)
    {
        this.stage = node;
        // this.TopLayar.name = "TopLayar";
        // this.stage.addChild(this.TopLayar);
        // // this.NormalLayer.name = "NormalLayer";
        // this.stage.addChild(this.NormalLayer);
        // // this.BottomLayer.name = "BottomLayer";
        // this.stage.addChild(this.BottomLayer);
        // this.FrontLayer.name = "FrontLayer";
        this.stage.addChild(this.FrontLayer);
        this.FrontLayer.setSiblingIndex(4);
        // this.TopLayar.setSiblingIndex(3);
        // this.NormalLayer.setSiblingIndex(2);
        // this.BottomLayer.setSiblingIndex(1);
    }

    /**
     * 注册自定义层级
     * @param uiName ui名字
     * @param node ui挂载节点
     */
    RegFreeLayer(uiName : EnUI, node : Node)
    {
        this.freeLayer.set(uiName, node);
    }

    SetLayer(type : EnUILayer, sprite : Node)
    {
        switch(type)
        {
            case EnUILayer.Top:
                {
                    this.TopLayar.addChild(sprite);
                    break;
                }
            case EnUILayer.Normal:
                {
                    this.NormalLayer.addChild(sprite);
                    break;
                }
            case EnUILayer.Bottom:
                {
                    this.BottomLayer.addChild(sprite);
                    break;
                }
            case EnUILayer.Free:
                {
                    let node = this.freeLayer.get(sprite.name);
                    if (node)
                    {
                        node.addChild(sprite);
                        this.freeLayer.delete(sprite.name);
                    }
                    break
                }
            case EnUILayer.Front:
                this.FrontLayer.addChild(sprite)
                break
        }
    }
 }
